package swd.work.generator.v2.java.bean;

import java.io.*;

import com.pub.database.*;

import swd.work.generator.v2.java.base.*;

public class ActionBean extends BeanExtend implements BeanImplement {

    public ActionBean(QueryBean pqbnDAO, String pstrUserNO) throws Exception {
        super(pqbnDAO, pstrUserNO);
    }

    /**
     * outputActionMethod
     *
     * @param lbwrPrgNO BufferedWriter
     */
    private void outputActionMethod(BufferedWriter pbwrPrgNO, String pstrSysNO,String pstrPrgNO) throws
            Exception {

        try{
            //output action method
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                                 "public String action(javax.servlet.jsp.PageContext pageContext){",
                                 1);

            //output request and response
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                                 "HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();",
                                 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                                 "HttpServletResponse response=(HttpServletResponse)pageContext.getResponse();",
                                 1);

            //out get view object from request attribute
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                                 "View view=(View)request.getAttribute(\"view\");",
                                 2);

            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                                 "//將MultiLang.properties的屬性值讀到對應的view.setter",
                                 2);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                                 "view.setMultiLang();",
                                 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                                 "//將DB的connect info 讀至view的setter",
                                 2);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                                 "view.setDBProperties();",
                                 1);

            //output check login
            outputCheckLoginCode(pbwrPrgNO, pstrPrgNO);

            //output get login user empno
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                    "view.setLoginUserNO(request.getSession().getAttribute(\"u_id\").toString());",
                                 2);

            //output result string varible
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                                 "String lstrResult=null;", 1);

            //輸出可動態執行java code 的物件
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "Interpreter lbhsIPR = new Interpreter();//可動態執行字串中的程式碼物件", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "StringBuffer lstbDynamicCode=new StringBuffer();", 1);

            //output initial QueryBean
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "gqbnDAO=new QueryBean(view.getDb_alias(),view.getDb_debug(),view.getSource_charset(),view.getTarget_charset());",
                                 2);

            //output inital Business bean
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                                 "Business business=new Business(gqbnDAO);", 1);

            //判斷是否需簽核流程  check need approval flow
            if (super.gblnFlowYN) {
                super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                                     "Flow flow=new Flow(\"" + super.gstrFlowCode + "\",\"" + super.getFlowMasterTable(super.gstrFlowCode) + "\",business);", 1);
            }

            //output try block
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "try{", 2);

            //簽核頁且簽核流程 check need approval flow and its a approval page
            if(super.gblnFlowYN && super.gstrPrgKind.equals("Y")){
                //output get flow seqno
                super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "//若沒有傳入seqno，則再query wkf_jst_eng一次(seqno 為call floweng需傳入的參數)", 1);
                super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "if(view.getSeqno()==null){", 1);
                super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "view.setSeqno(flow.readFlowSeqno(view,\"N\"));", 1);
                super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "if(view.getSeqno()==null){", 2);
                super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "view.setOnloadScript(\"alert('\" + view.getMessage(\"not_seqno\") +\"');window.close();\");", 1);
                super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "return \"\";", 1);
                super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
                super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
                //output check r_empno is login user
                super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "//若沒有傳入seqno，則再query wkf_jst_eng一次(seqno 為call floweng需傳入的參數)", 2);
                super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "if(!flow.checkJSTEmpno(view)){", 1);
                super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "view.setOnloadScript(\"alert('\" + view.getMessage(\"not_r_empno\") + \"');window.close();\");", 1);
                super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "return \"\";", 1);
                super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
            }

            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "//需先定義字串中的物件變數", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "lbhsIPR.set(\"business\",business);", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "lbhsIPR.set(\"view\", view);", 1);

            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "//利用AJAX即時讀取資料", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "if(view.getPrgAction()!=null && view.getPrgAction().equals(\"getData\")){", 2);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "lstrResult=business.getData(view);", 1);

            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "pageContext.getOut().clear();", 2);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "pageContext.getOut().write(lstrResult);", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "pageContext.getOut().flush();", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "return \"login\";", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);

            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "//每次submit一開始都要做的事", 2);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "lstrResult = business.actionForInit(view);", 1);

            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "//執行各動作對應的method", 2);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "if(view.getPrgAction()!=null){", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "lstbDynamicCode.append(\"business.actionFor\");", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "lstbDynamicCode.append(view.getPrgAction().substring(0,1).toUpperCase());", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "lstbDynamicCode.append(view.getPrgAction().substring(1));", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "lstbDynamicCode.append(\"(view);\");", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "//執行字串中的動態程式碼", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "lstrResult=lbhsIPR.eval(lstbDynamicCode.toString()).toString();", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);

            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "//每次submit最後要做的事", 2);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "lstrResult = business.actionForEnd(view);", 1);

            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "//依prgAction與prgStatus設定要開/關那些button", 2);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "business.setButtonShow(view,view.getPrgAction(),view.getPrgStatus());", 1);

            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "//依prgAction與prgStatus設定那些欄位為唯讀", 2);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "business.setReadonlyFields(view,view.getPrgAction(),view.getPrgStatus());", 1);

            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "//依prgAction與prgStatus設定要秀或隱藏那些欄位", 2);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "business.setShowFields(view,view.getPrgAction(),view.getPrgStatus());", 1);

            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "return \"\";", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                                 "catch(Exception e){", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                                 "view.setOnloadScript(\"alert('\" + e.toString().replaceAll(\"'\",\"′\") + \", please contact IT.');\");",
                                 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "return \"\";", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "finally{", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "try{", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                                 "if(gqbnDAO!=null){", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "gqbnDAO.close();",
                                 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                                 "catch(Exception e){", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                    "System.out.println(\"can not close querybean in Action.java\");",
                                 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
        }
        catch(Exception e){
            throw new Exception("ActionBean.outputActionMethod exception==>" + e.toString());
        }
    }

    /**
     * outputCheckLoginCode
     *
     * @param pbwrPrgNO BufferedWriter
     * @param pstrPrgNO String
     */
    private void outputCheckLoginCode(BufferedWriter pbwrPrgNO,
                                      String pstrPrgNO) throws Exception {
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "(new WebFunction()).setPortal_to_session(request); //先把cookie轉session", 1);

        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "//判斷session u_id 若為空值，則導回 login page", 2);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "if (request.getSession().getAttribute(\"u_id\")==null) {", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "try{", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "request.getRequestDispatcher((new StringBuffer(\"login.jsp\")).insert(0,\"/\").toString()).forward(request,response);", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "return \"login\";", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "catch(Exception le){", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "view.setOnloadScript(\"can not redirect to login page...please contact IT\");", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "return \"\";", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
    }

    /**
     * outputFlowAppCondition
     *
     * @param pbwrPrgNO BufferedWriter

    private void outputFlowAppCondition(BufferedWriter pbwrPrgNO) {
        Hashtable lhtFlowNO = null;
        Hashtable lhtFlowNOData = null;
        int lintFlowNO_cnt = 0;
        String lstrFlowCode=gstrSysNO.toUpperCase();
        String lstrFlowNO=null;

        try {
            lhtFlowNO = gqbnDAO.hashtableSQL(
                    "select flow_no from floweng_setup where flow_code = '" +
                    lstrFlowCode + "'");

            while (lhtFlowNO != null &&
                   lhtFlowNO.get(String.valueOf(lintFlowNO_cnt)) != null) {

                lhtFlowNOData = (Hashtable) lhtFlowNO.get(String.valueOf(
                        lintFlowNO_cnt));

                lstrFlowNO=lhtFlowNOData.get("FLOW_NO").toString();
                lintFlowNO_cnt++;

                super.outputJavaCode(pbwrPrgNO,gstbTabStr,gstrTabChar,"if(view.getPrgAction()!=null && view.getPrgAction().equals(\"approve\") && view.getstatus().equals(\"" + lstrFlowNO + "\")){",1);
                super.outputJavaCode(pbwrPrgNO,gstbTabStr,gstrTabChar,"view.setOnloadScript(\"alert('\" + flow.appFlow" + lstrFlowNO + "Action() + \"')\");",1);
                super.outputJavaCode(pbwrPrgNO,gstbTabStr,gstrTabChar,"}",1);
                super.outputJavaCode(pbwrPrgNO,gstbTabStr,gstrTabChar,"if((view.getPrgAction()!=null && view.getPrgAction().equals(\"pre_app\")) && view.getstatus()!=null && view.getstatus().equals(\"" + lstrFlowNO + "\")){",1);
                super.outputJavaCode(pbwrPrgNO,gstbTabStr,gstrTabChar,"flow.preAppFlow" + lstrFlowNO + "();",1);
                super.outputJavaCode(pbwrPrgNO,gstbTabStr,gstrTabChar,"}",1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (lhtFlowNOData != null) {
                lhtFlowNOData.clear();
            }
            lhtFlowNOData = null;
            if (lhtFlowNO != null) {
                lhtFlowNO.clear();
            }
            lhtFlowNO = null;
        }
    }
     */
    /**
     * outputClassVarible
     *
     * @param pbwrPrgNO BufferedWriter
     */
    private void outputClassVarible(BufferedWriter pbwrPrgNO) throws Exception {
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "QueryBean gqbnDAO=null;", 1);
    }

    public String createBean(String pstrPrgNO) {
        String lstrCreateResult = "";
        String lstrBeanFileName = "Action.java";
        BufferedWriter lbwrPrgNO = null;

        try {
            super.setBaseVarible(pstrPrgNO);

            lbwrPrgNO = super.getFwrPrgNO(lstrBeanFileName, "JSP_FRAME_PATH",
                                          "\\com\\swd\\" + getSysNO(gstrUserNO) +
                                          "\\" + pstrPrgNO + "\\", gstrUserNO);

            super.outputBeanHeader(lbwrPrgNO, gstrSysNO,
                                   pstrPrgNO, lstrBeanFileName, gstbTabStr,
                                   gstrTabChar);



            //output Classify varible
            outputClassVarible(lbwrPrgNO);

            //output Action method
            outputActionMethod(lbwrPrgNO, gstrSysNO,pstrPrgNO);

            //output class end single
            super.outputJavaCode(lbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                lbwrPrgNO.flush();
                lbwrPrgNO.close();
                lbwrPrgNO = null;
            } catch (Exception e) {
                System.out.println("cant flush Action.java File!!");
                e.printStackTrace();
            }  
        }
        
        return lstrCreateResult;
    }


}
